﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using DevExpress.XtraEditors.Controls;

namespace TCCT_CTC_CSDLDangVienV1
{
    public partial class frmCanBoCot : frmBase
    {
        public string SelectedDauMoiID = "";
        public string FilterString = "";
        public string ConditionString = "";

        public frmCanBoCot()
        {
            InitializeComponent();
        }

        private void frmCanBoCot_Load(object sender, EventArgs e)
        {

            if (SelectedDauMoiID == "")
                SelectedDauMoiID = MyCommon.CurrentUserDauMoiID;
            ApplyUI();

            BindDataToColtrol();


        }

        private void ApplyUI()
        {



            UI.Button.ApplyAccept(btnAccept);
            UI.Button.ApplyCancel(btnCancel);


        }
        private void BindDataToColtrol()
        {
            lstCot.DataSource = SqlHelper.ExecuteDataTable(CommandType.Text, "Select * From tblCanBoCot Order By ID");
            lstCot.DisplayMember = "TieuDe";
            lstCot.ValueMember = "TenCot";

            lstCotChon.DataSource = SqlHelper.ExecuteDataTable(CommandType.Text, "Select * From tblCanBoCot Where 1=0 Order By ID");
            lstCotChon.DisplayMember = "TieuDe";
            lstCotChon.ValueMember = "TenCot";

        }


        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (lstCotChon.ItemCount == 0)
            {
                MyCommon.ShowMessage("Bạn chưa chọn cột nào");
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnChonTatCa_Click(object sender, EventArgs e)
        {

            lstCotChon.DataSource = SqlHelper.ExecuteDataTable(CommandType.Text, "Select * From tblCanBoCot Order By ID");
            lstCotChon.DisplayMember = "TieuDe";
            lstCotChon.ValueMember = "TenCot";

        }

        private void btnBoChonHet_Click(object sender, EventArgs e)
        {

            lstCotChon.DataSource = SqlHelper.ExecuteDataTable(CommandType.Text, "Select * From tblCanBoCot Where 1=0 Order By ID");
            lstCotChon.DisplayMember = "TieuDe";
            lstCotChon.ValueMember = "TenCot";

        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            //Kiểm tra xem tồn tại hay chưa:
            for (int i = 0; i < lstCotChon.ItemCount; i++)
            {
                if (lstCot.SelectedValue.ToString() == lstCotChon.GetItemValue(i).ToString())
                {
                    MyCommon.ShowMessage("Đã tồn tại cột này rồi");
                    return;
                }
            }

            DataRow dr = ((DataTable)lstCotChon.DataSource).NewRow();
            dr["TenCot"] = lstCot.SelectedValue.ToString();
            dr["TieuDe"] = lstCot.Text;
            ((DataTable)lstCotChon.DataSource).Rows.Add(dr);
        }

        private void btnBoChon_Click(object sender, EventArgs e)
        {
            if (lstCotChon.ItemCount == 0)
            {
                MyCommon.ShowMessage("Không có dữ liệu để xóa");
                return;
            }
            ((DataTable)lstCotChon.DataSource).Rows.RemoveAt(lstCotChon.SelectedIndex);
            MyCommon.ShowMessage("Thực hiện thành công");
        }

    }
}